Test Series - Data Structure

Test Number 30/115

Q: The optimal data structure used to solve Tower of Hanoi is _________
A. Tree
B. Heap
C. Priority queue
D. Stack
Solution: The Tower of Hanoi involves moving of disks ‘stacked’ at one peg to another peg with respect to the size constraint. It is conveniently done using stacks and priority queues. Stack approach is widely used to solve Tower of Hanoi.
Q: Select the appropriate code for the recursive Tower of Hanoi problem.(n is the number of disks)
A. public void solve(int n, String start, String auxiliary, String end) { if (n == 1) { System.out.println(start + " -> " + end); } else { solve(n - 1, start, end, auxiliary); System.out.println(start + " -> " + end); solve(n - 1, auxiliary, start, end); } }
B. public void solve(int n, String start, String auxiliary, String end) { if (n == 1) { System.out.println(start + " -> " + end); } else { solve(n - 1, auxiliary, start, end); System.out.println(start + " -> " + end); } }
C. public void solve(int n, String start, String auxiliary, String end) { if (n == 1) { System.out.println(start + " -> " + end); } else { System.out.println(start + " -> " + end); solve(n - 1, auxiliary, start, end); } }
D. public void solve(int n, String start, String auxiliary, String end) { if (n == 1) { System.out.println(start + " -> " + end); } else { solve(n - 1, start, end, auxiliary); System.out.println(start + " -> " + end); } }
Solution: First transfer all the diska to the auxiliary and then to the end peg, this is achieved by making auxiliary peg as the end peg in the first recursive call, in the second recursive call, the auxiliary becomes the start peg from where the disks are transferred to the end peg.
Q: Which among the following is not a palindrome?
A. Madam
B. Dad
C. Malayalam
D. Maadam
Solution: A palindrome is a string that reads the same forward and backward, Madam, Dad and Malayalam are palindromes where as Maadam is not a palindrome.
Q: Which data structure can be used to test a palindrome?
A. Tree
B. Heap
C. Stack
D. Priority queue
Solution: Stack is a convenient option as it involves pushing and popping of characters.
Q: Select the appropriate code which tests for a palindrome.
A. public static void main(String[] args) { System.out.print("Enter any string:"); Scanner in=new Scanner(System.in); String input = in.nextLine(); Stack stk = new Stack(); for (int i = 0; i < input.length(); i++) { stk.push(input.charAt(i)); } String reverse = ""; while (!stk.isEmpty()) { reverse = reverse + stk.pop(); } if (input.equals(reverse)) System.out.println("palindrome"); else
B. public static void main(String[] args) { System.out.print("Enter any string:"); Scanner in=new Scanner(System.in); String input = in.nextLine(); Stack stk = new Stack(); for (int i = 0; i < input.length(); i++) { stk.push(input.charAt(i)); } String reverse = ""; while (!stk.isEmpty()) { reverse = reverse + stk.peek(); } if (input.equals(reverse)) System.out.println("palindrome"); else
C. public static void main(String[] args) { System.out.print("Enter any string:"); Scanner in=new Scanner(System.in); String input = in.nextLine(); Stack stk = new Stack(); for (int i = 0; i < input.length(); i++) { stk.push(input.charAt(i)); } String reverse = ""; while (!stk.isEmpty()) { reverse = reverse + stk.pop(); stk.pop(); } if (input.equals(reverse)) System.out.println("palindrome");
D. public static void main(String[] args) { System.out.print("Enter any string:"); Scanner in=new Scanner(System.in); String input = in.nextLine(); Stack stk = new Stack(); for (int i = 0; i < input.length(); i++) { stk.push(input.charAt(i)); } String reverse = ""; while (!stk.isEmpty()) { reverse = reverse + stk.pop(); stk.pop(); } if (!input.equals(reverse)) System.out.println("palindrome");
Solution: Push all the characters in the input string to a stack, now pop them and append to a new string which is checked for equality with the original string.
Q: What is the number of moves required to solve Tower of Hanoi problem for k disks?
A. 2k – 1
B. 2k + 1
C. 2k + 1
D. 2k – 1
Solution: Tracing of the moves in the above ToH problem will prove this result, instead you can simply add a count for each recursive call to check the number of moves.
Q: Select the appropriate code which reverses a word.
A. public String reverse(String input) { for (int i = 0; i < input.length(); i++) { stk.push(input.charAt(i)); } String rev = ""; while (!stk.isEmpty()) { rev = rev + stk.peek(); } return rev; }
B. public String reverse(String input) { for (int i = 0; i < input.length(); i++) { stk.push(input.charAt(i)); } String rev = ""; while (!stk.isEmpty()) { rev = rev + stk.pop(); } return rev; }
C. public String reverse(String input) { for (int i = 0; i < input.length(); i++) { stk.push(input.charAt(i)); } String rev = ""; while (!stk.isEmpty()) { rev = rev + stk.pop(); } }
D. public String reverse(String input) { for (int i = 0; i < input.length(); i++) { stk.push(input.charAt(i)); } String rev = ""; while (!stk.isEmpty()) { rev = rev + stk.pop(); stk.pop(); } return rev; }
Solution: Although, it is possible to reverse the string without using stack, it is done by looping through the string from the end character by character.
Q: 
A. 
B. 
C. 
D. 
Solution: 

You Have Score    /8